package com.zyh.homework.test2;

/**
 * @Author zyh
 * @Date 2024/8/7 17:17
 * @Description
 */

public class Printer {
    private Integer index=1;

    public Printer(Integer index) {
        this.index = index;
    }

    public Printer() {
    }

    //打印数字
    public synchronized void print(int i){
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (index%3==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(i);
        index++;
        this.notifyAll();
    }

    //打印字母
    public synchronized void print(char c){
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //判断index是否不能被3整除
        while(index%3!=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(c);
        index++;
        //唤醒数字线程
        this.notifyAll();
    }
}
